﻿
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestNewLandCloud
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

           
            string token = Common.GetToken(App.ServerIP, App.UserName, App.Pwd, App.ProjectTag);


            HttpHelper helper = new HttpHelper(App.ServerIP);

            helper.RequestData("OnOff", "Device", "OperationActuator?actuatorId=" + 7 + "&actuatorStatus=" + 1, "AccessToken", token);
            string strResult = helper.ResultMsg;

            bool result = false;
            if (!string.IsNullOrWhiteSpace(strResult))
            {
                ResponseModel response = JsonConvert.DeserializeObject<ResponseModel>(strResult);
                if (response != null)
                {
                    if (response.Status == ResultStatus.Success)
                        result = true;
                }
            }
           
      
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.11.138/api/Account/Logon");

            request.Method = "POST";
            request.ContentType = "application/json";
            request.Accept = "application/json;charset=utf-8";


            Stream reqStream = request.GetRequestStream();

            System.Web.Script.Serialization.JavaScriptSerializer javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            LogInInfo logInInfo = new LogInInfo();
            logInInfo.UserName = "Boss";
            logInInfo.Password = "987654321";
            logInInfo.ProjectTag = "case";
            string strJSON = javaScriptSerializer.Serialize(logInInfo);//"{\"UserName\": \"Boss\", \"Password\": \"987654321\", \"ProjectTag\": \"case\"}";
            byte[] byteData = Encoding.UTF8.GetBytes(strJSON);
            reqStream.Write(byteData, 0, byteData.Length);


            HttpWebResponse respons = (HttpWebResponse)request.GetResponse();

            Stream stream = respons.GetResponseStream();
            StreamReader stream_Read = new StreamReader(stream, Encoding.UTF8);
            string strs = stream_Read.ReadToEnd();


           

            if (!string.IsNullOrWhiteSpace(strs))
            {


                ResponseModel response = javaScriptSerializer.Deserialize<ResponseModel>(strs);//DeserializeObject<ResponseModel>(strResult);

                if (response != null)
                {
                    if (response.Status == ResultStatus.Success)
                    {
                        if (response.AppendData != null && !string.IsNullOrWhiteSpace(response.AppendData.ToString()))
                        {
                            System.Collections.Generic.Dictionary<string, object> dict = (System.Collections.Generic.Dictionary<string, object>)response.AppendData;
                            object testObject = dict["AccessToken"];
                            string strValue = (string)testObject;
                            txt_code.Text = strValue;
                        }
                    }
                }
            }


        }
    }
}
